const sqlOptions = require("../../sql");
const {listToTree} = require("../../utils");
const home = require("express").Router()

//首页轮播图
home.get("/carousel", async (req, res) => {
    try {
        const carouselList = await req.db.query(sqlOptions({
            name: "sms_carousel",
            method: "list",
            data: {}
        }))
        return res.status(200).send({
            code: 200,
            message: "",
            data: carouselList.map(item => ({
                click_count: item.click_count,
                carousel_order: item.carousel_order,
                carousel_url: item.carousel_url,
                carousel_img: item.carousel_img
            }))
        })
    } catch (e) {
        return res.status(500).send({
            code: '500',
            message: '系统错误',
            data: ''
        })
    }
})
//首页分类
home.get("/category", async (req, res) => {
    try {
        const list = await req.db.query(sqlOptions({
            name: "sms_category",
            method: "list",
            data: {}
        }))
        const tree = listToTree(list, {
            parentKey: 'parent_id',
            idKey: "category_id",
            order: "category_order"
        })
        return res.status(200).send({
            code: 200,
            message: "",
            data: tree
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})
// 推荐
home.get("/recommend", async (req, res) => {
    try {
        const list = await req.db.query(sqlOptions({
            data: {},
            name: "sms_recommend",
            method: 'list'
        }))
        // console.log(list)
        const brandList = await req.db.query(sqlOptions({
            data: {},
            name: "sms_brand",
            method: 'list'
        }))
        const categoryList = await req.db.query(sqlOptions({
            data: {},
            name: "sms_category",
            method: 'list'
        }))
        return res.status(200).send({
            code: 200,
            message: "",
            data: list.map(item => {
                return {
                    ...item,
                    brand_name: brandList.filter(i => i.brand_id === item.brand_id )[0].brand_name,
                    category_name: categoryList.filter(i => i.category_id === item.category_id )[0].category_name,
                }
            })
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})

// 根据商品id获取商品信息
home.get("/product", async (req, res) => {
    try {
        const {product_id} = req.query
        // 商品表数据
        const productList = await req.db.query(sqlOptions({
            name: "sms_product",
            method: "list",
            data: {
                product_id
            }
        }))
        if (productList.length) {
            // 颜色
            const sms_product_color = await req.db.query(sqlOptions({
                name: "sms_product_color",
                method: "list",
                data: {
                    product_id
                }
            }))
            // 详情
            const sms_product_info = await req.db.query(sqlOptions({
                name: "sms_product_info",
                method: "list",
                data: {
                    product_id
                }
            }))
            // 轮播图
            const sms_product_pic = await req.db.query(sqlOptions({
                name: "sms_product_pic",
                method: "list",
                data: {
                    product_id
                }
            }))
            // 图片
            const sms_product_pic_info = await req.db.query(sqlOptions({
                name: "sms_product_pic_info",
                method: "list",
                data: {
                    product_id
                }
            }))
            const brandList = await req.db.query(sqlOptions({
                data: {},
                name: "sms_brand",
                method: 'list'
            }))
            const categoryList = await req.db.query(sqlOptions({
                data: {},
                name: "sms_category",
                method: 'list'
            }))
            return res.status(200).send({
                code: 200,
                message: '',
                data: {
                    product: productList.map(item => {
                        let brand_name;
                        if (brandList.length) {
                            brand_name = brandList.filter(i => i.brand_id === item.brand_id )[0].brand_name
                        }
                        let category_name;
                        if (categoryList.length) {
                            category_name = categoryList.filter(i => i.category_id === item.category_id )[0].category_name
                        }
                        return {
                            ...item,
                            brand_name,
                            category_name,
                        }
                    })[0],
                    info: {
                        pic: sms_product_pic_info.map(item => item.pic),
                        info: sms_product_info.map(item => [item.label, item.value])
                    },
                    color: sms_product_color,
                    pic: sms_product_pic.map(item => item.product_pic_url)
                }
            })
        }
        return res.status(200).send({
            code: 400,
            message: "没有该商品",
            data: ""
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})
// 根据商品id获取商品评价
home.post("/comment", async (req, res) => {
    try {
        const {product_id} = req.body
        const list = await req.db.query(sqlOptions({
            name: 'oms_product_comment',
            method: "list",
            data: {
                product_id
            }
        }))
        return res.status(200).send({
            code: 200,
            message: "",
            data: list
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})

module.exports = home